package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.SkuService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class SkuController {

    //根据spuId获取sku图片列表
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList (@PathVariable Long spuId){
        List<SpuImage> spuImageList = skuService.spuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //根据spuId获取销售属性
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList (@PathVariable Long spuId){
        List<SpuSaleAttr> spuSaleAttrList= skuService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    //添加sku
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        skuService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    //获取sku分页列表
    @GetMapping("/list/{page}/{limit}")
    public Result list(@PathVariable Integer page,
                       @PathVariable Integer limit){
        IPage<SkuInfo> list = skuService.list(page, limit);
        return Result.ok(list);
    }

    //上架
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        skuService.onSale(skuId);
        return Result.ok();
    }
    //下架
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        skuService.cancelSale(skuId);
        return Result.ok();
    }

    @Autowired
    private SkuService skuService;

}
